package com.orm.analysis.service;

import com.orm.analysis.dao.AddressDAO;
import com.orm.analysis.dao.ContactInfoDAO;
import com.orm.analysis.dao.CustomerDAO;
import com.orm.analysis.dao.OrderDAO;
import com.orm.analysis.model.*;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


public class CustomerService implements GenericService<Customer> {


    private static Logger log = Logger.getLogger(CustomerService.class);

    private String customerDataFielDir;

    private CustomerDAO dao;

    private ContactInfoDAO contactInfoDAO;

    private OrderDAO orderDAO;


    public String getCustomerDataFielDir() {
        return customerDataFielDir;
    }

    public void setCustomerDataFielDir(String customerDataFielDir) {
        this.customerDataFielDir = customerDataFielDir;
    }

    public CustomerDAO getDao() {
        return dao;
    }

    public void setDao(CustomerDAO dao) {
        this.dao = dao;
    }

    public OrderDAO getOrderDAO() {
        return orderDAO;
    }

    public void setOrderDAO(OrderDAO orderDAO) {
        this.orderDAO = orderDAO;
    }

    public ContactInfoDAO getContactInfoDAO() {
        return contactInfoDAO;
    }

    public void setContactInfoDAO(ContactInfoDAO contactInfoDAO) {
        this.contactInfoDAO = contactInfoDAO;
    }

    public Customer create(Customer entity) {
        dao.save(entity);
        return entity;
    }

    public void delete(Object id) {

        dao.delete(id);
    }


    public Customer update(Customer entity) {
        dao.update(entity);
        return entity;
    }

    public List<Customer> list(int page, int size) {
        return dao.list(page,size);
    }

    public Customer  getById(Integer id) {
         return dao.getById(id);
     }

    public Customer  getById(Customer entity) {
             return dao.getById(entity.getId());
    }


    public void executeImproter() throws ParseException,IOException{
        log.info("File  >>" + customerDataFielDir);
              BufferedReader br = null;

              br = new BufferedReader(new FileReader(customerDataFielDir));

              String line = br.readLine();
                line = br.readLine();

              while (line != null) {
                  //ID( not need)  FIRST_NAME,LAST_NAME,REGISTRATION_DATE,contactInfo, CUSTOMER_CODE,CREDIT_LIMIT,custOrders~custOrders
                  Customer customer  = new Customer();

                  String[] data = line.split("[,]");



                  customer.setFirstName(data[0]);
                  customer.setLastName(data[1]);

                  SimpleDateFormat simpleDateFormat = new SimpleDateFormat(GlobalConst.DATE_PATTERN) ;

                  customer.setRegistrationDate(simpleDateFormat.parse(data[2].trim()));

                  if(data[3].trim().length()>0){
//                      customer.setContactInfo(contactInfoDAO.getById(Integer.valueOf((data[3].trim()))));
                      customer.setContactInfo(new ContactInfo( Integer.valueOf((data[3].trim()))));
                  }


                  customer.setCustomerCode(data[4]);
                  customer.setCreditLimit(Double.valueOf(data[5]));

                  if (data.length>6 && data[6]!=null &&  data[6].length() > 0) {
                      List<CustOrder> custOrderList = new ArrayList<CustOrder>();
                      String[] custOrderIdArray = data[6].split("[~]");
                      for (int i = 0; i < custOrderIdArray.length; i++) {
                          custOrderList.add(orderDAO.getById(Integer.valueOf(custOrderIdArray[i])));
                      }
                      if(custOrderIdArray.length>0){
                          customer.setCustOrders(custOrderList);
                      }
                  }


                  dao.save(customer);

                  line = br.readLine();

              }

              br.close();
    }
}
